package com.server.commons.security.Aspect;

import cn.hutool.core.util.ReflectUtil;
import com.server.common.security.code.anntation.FileRelativePath;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;

@Aspect
@Component
public class FileRelativePathAspect {

    @AfterReturning("@annotation(com.server.common.security.code.anntation.FileRelativePath)")
    public void afterReturning(JoinPoint point) throws Throwable {

        Object[] args = point.getArgs();

        Object arg = args[0];

        A(arg);

    }

    private void A(Object obj){
        Field[] fields = ReflectUtil.getFields(obj.getClass());
        for (Field field:fields){
            Object fieldValue = ReflectUtil.getFieldValue(obj, field);

            if(ObjectUtils.isEmpty(fieldValue)){
                continue;
            }

            if(isPrimitive(field.getType())){

                FileRelativePath annotation = field.getAnnotation(FileRelativePath.class);

                //
                

                continue;
            }


            A(fieldValue);
        }

    }

    private static boolean isPrimitive(Class<?> clazz) {
        return clazz.isPrimitive() || clazz == Integer.class ||
                clazz == Long.class || clazz == Float.class ||
                clazz == Double.class || clazz == Boolean.class ||
                clazz == Byte.class || clazz == Character.class ||
                clazz == Short.class ||
                clazz == int.class || clazz == long.class ||
                clazz == float.class || clazz == double.class ||
                clazz == boolean.class || clazz == byte.class ||
                clazz == char.class || clazz == short.class ||
                (clazz.isArray() && clazz.getComponentType().isPrimitive()) ||
                clazz.getName().startsWith("java.");
    }

}
